package com.qin.malldiscount.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qin.malldiscount.dao.SmsCouponDao;
import com.qin.malldiscount.dao.SmsCouponProductCategoryRelationDao;
import com.qin.malldiscount.dao.SmsCouponProductRelationDao;
import com.qin.malldiscount.domain.SmsCoupon;
import com.qin.malldiscount.domain.SmsCouponProductCategoryRelation;
import com.qin.malldiscount.domain.SmsCouponProductRelation;
import com.qin.malldiscount.dto.SmsCouponParam;
import com.qin.malldiscount.mapper.SmsCouponProductCategoryRelationMapper;
import com.qin.malldiscount.mapper.SmsCouponProductRelationMapper;
import com.qin.malldiscount.service.SmsCouponService;
import com.qin.malldiscount.mapper.SmsCouponMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author 覃剑
* @description 针对表【sms_coupon(优惠券表)】的数据库操作Service实现
* @createDate 2022-11-23 16:35:39
*/
@Service
public class SmsCouponServiceImpl extends ServiceImpl<SmsCouponMapper, SmsCoupon>
    implements SmsCouponService{
    
    @Autowired
    private SmsCouponMapper couponMapper;
    @Autowired
    private SmsCouponProductRelationDao productRelationDao;
    @Autowired
    private SmsCouponProductCategoryRelationDao productCategoryRelationDao;
    @Autowired
    private SmsCouponProductCategoryRelationMapper productCategoryRelationMapper;
    @Autowired
    private SmsCouponProductRelationMapper productRelationMapper;
    @Autowired
    private SmsCouponDao couponDao;

    /**
     * 添加优惠券
     */
    @Override
    public int create(SmsCouponParam couponParam) {
        couponParam.setCount(couponParam.getPublishCount());
        couponParam.setUseCount(0);
        couponParam.setReceiveCount(0);
        //插入优惠券表
        int count = couponMapper.insert(couponParam);
        //插入优惠券和商品关系表
        if(couponParam.getUseType().equals(2)){
            for(SmsCouponProductRelation productRelation:couponParam.getProductRelationList()){
                productRelation.setCouponId(couponParam.getId());
            }
            productRelationDao.insertList(couponParam.getProductRelationList());
        }
        //插入优惠券和商品分类关系表
        if(couponParam.getUseType().equals(1)){
            for (SmsCouponProductCategoryRelation couponProductCategoryRelation : couponParam.getProductCategoryRelationList()) {
                couponProductCategoryRelation.setCouponId(couponParam.getId());
            }
            productCategoryRelationDao.insertList(couponParam.getProductCategoryRelationList());
        }
        return count;
    }

    /**
     * 根据优惠券id删除优惠券
     */
    @Override
    public int delete(Long id) {
        //删除优惠券
        int count = couponMapper.deleteById(id);
        //删除商品关联
        //删除商品分类关联
        deleteProductRelation(id);
        return count;
    }
    

    //删除商品关联
    private void deleteProductCategoryRelation(Long id) {
        QueryWrapper<SmsCouponProductRelation> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        int delete = productRelationMapper.delete(wrapper);
    }

    
    //删除商品分类关联
    private void deleteProductRelation(Long id) {
        QueryWrapper<SmsCouponProductCategoryRelation> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        int delete = productCategoryRelationMapper.delete(wrapper);
    }

    /**
     * 根据优惠券id更新优惠券信息
     */
    @Override
    public int update(Long id, SmsCouponParam couponParam) {
        couponParam.setId(id);
        int count = couponMapper.updateById(couponParam);
        //更新后插入优惠券和商品关系表
        if(couponParam.getUseType().equals(2)){
            for(SmsCouponProductRelation productRelation:couponParam.getProductRelationList()){
                productRelation.setCouponId(couponParam.getId());
            }
            deleteProductRelation(id);
            productRelationDao.insertList(couponParam.getProductRelationList());
        }
        //更新后插入优惠券和商品分类关系表
        if(couponParam.getUseType().equals(1)){
            for (SmsCouponProductCategoryRelation couponProductCategoryRelation : couponParam.getProductCategoryRelationList()) {
                couponProductCategoryRelation.setCouponId(couponParam.getId());
            }
            deleteProductCategoryRelation(id);
            productCategoryRelationDao.insertList(couponParam.getProductCategoryRelationList());
        }
        return count;
    }

    /**
     * 分页获取优惠券列表
     */
    @Override
    public Page<SmsCoupon> list(String name, Integer type, Integer pageSize, Integer pageNum) {
        Page<SmsCoupon> couponPage = new Page<>(pageNum, pageSize);
        QueryWrapper<SmsCoupon> wrapper = new QueryWrapper<>();
        if (StrUtil.isNotEmpty(name)){
            wrapper.like("name",name);
        }
        if (type!=null){
            wrapper.eq("type",type);
        }
        Page<SmsCoupon> page = couponMapper.selectPage(couponPage, wrapper);
        return page;
    }

    /**
     * 获取优惠券详情
     * @param id 优惠券表id
     */
    @Override
    public SmsCouponParam getItem(Long id) {
        return couponDao.getItem(id);
    }
}




